package Member1;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class Input extends JPanel {
	private JLabel lblNewLabel;
	private JTextField email;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JLabel label;
	private JTextField phone;
	private JTextField address;
	private JTextField name;
	private JTextField age;
	private JButton btnNewButton;

	Data6[] d;
	private JLabel status;
	/**
	 * Create the panel.
	 */
	public Input() {
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		add(getLblNewLabel());
		add(getEmail());
		add(getLblNewLabel_1());
		add(getLblNewLabel_2());
		add(getLblNewLabel_3());
		add(getLabel());
		add(getTextField_1_1());
		add(getTextField_2_1());
		add(getTextField_3_1());
		add(getTextField_4_1());
		add(getBtnNewButton());
		add(getStatus());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("성명");
			lblNewLabel.setBounds(12, 26, 50, 15);
		}
		return lblNewLabel;
	}
	private JTextField getEmail() {
		if (email == null) {
			email = new JTextField();
			email.setBounds(74, 98, 203, 21);
			email.setColumns(10);
		}
		return email;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("주소");
			lblNewLabel_1.setBounds(12, 51, 50, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("연락처");
			lblNewLabel_2.setBounds(12, 76, 50, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("이메일");
			lblNewLabel_3.setBounds(12, 101, 50, 15);
		}
		return lblNewLabel_3;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("나이");
			label.setBounds(12, 126, 50, 15);
		}
		return label;
	}
	private JTextField getTextField_1_1() {
		if (phone == null) {
			phone = new JTextField();
			phone.setColumns(10);
			phone.setBounds(73, 73, 204, 21);
		}
		return phone;
	}
	private JTextField getTextField_2_1() {
		if (address == null) {
			address = new JTextField();
			address.setColumns(10);
			address.setBounds(74, 48, 203, 21);
		}
		return address;
	}
	private JTextField getTextField_3_1() {
		if (name == null) {
			name = new JTextField();
			name.setColumns(10);
			name.setBounds(74, 23, 203, 21);
		}
		return name;
	}
	private JTextField getTextField_4_1() {
		if (age == null) {
			age = new JTextField();
			age.setColumns(10);
			age.setBounds(74, 123, 203, 21);
		}
		return age;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				try{
					Input i =  new Input();
					i.setD(d);
					// 데이터 입력 여부 체크
					boolean flag = true; // 모든 값을 입력했을 때
					if(name.getText().equals("")) flag=false;     //  빈공간이 생기면 안돌아간다.   플래그가 참이라면 입력되고 
					else if(address.getText().equals("")) flag=false;
			        else if(phone.getText().equals("")) flag=false;
			        else if(email.getText().equals("")) flag=false;
			        else if(age.getText().equals("")) flag=false;
			        
			        if(flag){
			        	Data6 data = new Data6();
			        	data.name = name.getText();
			        	data.address = address.getText();
			        	data.phone = phone.getText();
			        	data.email = email.getText();
			        	data.age = Integer.parseInt(age.getText());
					for(int pos=0;pos<d.length;pos++){
						if(d[pos] == null){
							d[pos] = data;
							
							name.setText("");
							address.setText("");
							phone.setText("");
							email.setText("");
							age.setText("");
							
							name.requestFocus(); // 커서이동
							status.setText("테이터가 추가되었습니다.");
							break;
						} 
					}
			
		
			        	
			        }
				}
			        catch(NumberFormatException e){
						JOptionPane.showMessageDialog(null, "나이는 숫자만 입력하세요.","경고",JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			btnNewButton.setBounds(74, 173, 97, 23);
		}
		return btnNewButton;
	}
	
	
	
	public void setD(Data6[] d){
		this.d = d;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("데이터 값을 입력하세요.");
			status.setBackground(Color.GREEN);
			status.setOpaque(true);
			status.setBounds(12, 271, 427, 21);
		}
		return status;
	}
}
